/*
 * BoundingBox.java
 *
 * Created on October 6, 2007, 7:16 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */


package flightlifter;



/**
 *
 * @author Matt
 */
public class BoundingBox {
    
    public PVector min, max;
   // boolean selected = false;
    /** Creates a new instance of BoundingBox */
    public BoundingBox(PVector min, PVector max) {
        this.min = min;
        this.max = max;
    }
    /*
     * Check if two Bounding Boxes interesect
     */
    public boolean intersect(BoundingBox bounds){
        boolean x = (max.x >= bounds.min.x) && (min.x <= bounds.max.x);
        boolean y = (max.y >= bounds.min.y) && (min.y <= bounds.max.y);
        boolean z = (max.z >= bounds.min.z) && (min.z <= bounds.max.z);
        return (x && y && z);
    }

    public boolean contains(PVector point){
        boolean x = (max.x >= point.x) && (min.x <= point.x);
        boolean y = (max.y >= point.y) && (min.y <= point.y);
        boolean z = (max.z >= point.z) && (min.z <= point.z);
        return (x && y && z);
    }
    
    public void draw(){

            

    
    }
    
    public void contain(Intersectable newObject){
        if (newObject.minBounds.x <= min.x) min.x = newObject.minBounds.x;
        if (newObject.minBounds.y <= min.y) min.y = newObject.minBounds.y;
        if (newObject.minBounds.z <= min.z) min.z = newObject.minBounds.z;

        if (newObject.maxBounds.x >= max.x) max.x = newObject.maxBounds.x;
        if (newObject.maxBounds.y >= max.y) max.y = newObject.maxBounds.y;
        if (newObject.maxBounds.z >= max.z) max.z = newObject.maxBounds.z; 
        
    }
    
    public boolean intersect(Ray ray){
        float t0  = ray.minDist;
        float t1  = ray.maxDist;
        
        
        float invRayDir;
        float tNear,tFar;
        
        //XAXIS
        invRayDir= 1.f/ray.dir.x;
        tNear = (min.x - ray.origin.x ) * invRayDir;
        tFar = (max.x - ray.origin.x) * invRayDir;
        if (tNear > tFar) {
            float temp = tFar;
            tFar = tNear;
            tNear = temp;
        } 
        t0 = tNear > t0 ? tNear : t0;
        t1 = tFar < t1 ? tFar : t1;
        if (t0 > t1) return false;
         
        //YAXIS
        invRayDir= 1.f/ray.dir.y;
        tNear = (min.y - ray.origin.y ) * invRayDir;
        tFar = (max.y - ray.origin.y) * invRayDir;
        if (tNear > tFar) {
            float temp = tFar;
            tFar = tNear;
            tNear = temp;
        } 
        t0 = tNear > t0 ? tNear : t0;
        t1 = tFar < t1 ? tFar : t1;
        if (t0 > t1) return false;
        
        //ZAXIS
        invRayDir= 1.f/ray.dir.z;
        tNear = (min.z - ray.origin.z ) * invRayDir;
        tFar = (max.z - ray.origin.z) * invRayDir;
        if (tNear > tFar) {
            float temp = tFar;
            tFar = tNear;
            tNear = temp;
        } 
        t0 = tNear > t0 ? tNear : t0;
        t1 = tFar < t1 ? tFar : t1;
        if (t0 > t1) return false;        
        
        ray.a = t0;
        ray.b = t1;
        
        return true;
        
    }
    
    
}
